var hidden1=Array();
var hidden2=Array();
webix.ready(function(){
  var my_template={
    template: "<span style='font-size:20px; font-weight:bold; color:#3498DB'; >机台入组</span>&nbsp;/&nbsp;&nbsp; MachineGroup",
    height:45,
    borderless:true 
  };
  
var button={
    type:"line",margin:20,css:"buttons",rows:
    [
      {view:"button",value:"<--",width:50,click:"addOne"},
      {view:"button",value:"-->",width:50,click:"clearOne"},
      {view:"button",value:"<<",width:50,click:"addAll"},
      {view:"button",value:">>",width:50,click:"clearAll"},
    ]
};
// 显示所有组
var list1={
  css:"list1",
  rows:[
    { margin:10,
      cols:[
        {
          view:"text",
          id:"add1",
          label:"组名:",
          placeholder:"请输入要添加的组名",
          width:240
        },
        {
          view:"button",
          width:70,
          value:"添加组",
          click:"addRow"
        }
      ]
    },
    {
        view:"text",
        id:"search1",
        label:"快速查找:",
        placeholder:"请输入查询的组名",
        width:320
    },
    {
      view:"list",
      width:320,
      id:"group",
      height:400,
      template:"#gourp_name#/#gourp_id#&nbsp;&nbsp;<button class='delbtn11 right2 style'>删除</button>",
      select:true,
      data:[],
      onClick: {// 删除工序事件
          "delbtn11": function(e, id, trg) {
        			 webix.confirm({
         		        title:"提示",
         		        text:"确定要删除吗?",
         		        ok:"确定", 
         		        cancel:"取消",
         		        callback:function(result){
         		        	if(result==true){
         		        		var list_index2=$$("group").getItem(id);// 点击的删除按钮所在工序索引

         		        		 webix.ajax().sync().post("delGourp", "gourp_id="+list_index2.gourp_id ,function(text, xml, xhr) {
         		        			 location.reload();
         		        		 });
         		        	}
         		        }	
         		        });
               
          
          }
          }
    }
]};
// 显示选中组的所有操作员
var list2={css:"list2",
  rows:[
  { margin:10,
    cols:[
      {
        view:"label",
        label:"已有机台:",
        width:100
      },
      {
        view:"label",
        id:"num",
        width:100
      }
    ]
  },
  {
    view:"list",
    width:320,
    height:400,
    id:"operater",
    template:"#machine_code#/#machine_id#",
    select:true,
    data:[]
  }
]};
// 显示所有操作员
var list3={css:"list3",
  rows:[
  {
    view:"text",
    id:"search2",
    placeholder:"请输入查询的机台",
    label:"快速查找:",
    width:320
  },
  {
    view:"list",
    width:320,
    height:400,
    select:true,
    multiselect:true,
    id:"allOperate",
    template:"#machine_code#/#machine_id#",
  }
]};

var body={
     // margin:10,
     css:"borderGroup",
     width:1400,
     rows:[
     { cols:[
      list1,
      list2,
      button,
      list3
    ]},
    {css:{"text-align":"right"},margin:10,
      cols:[
        {view:"button",value:"确定",width:60,click:"sure"}
      ]
    }
     ]
  };
  var web={
    container:"body",
    id:"mylayout",
    rows:[
      my_template,
      body
    ],
  };
  webix.ui(web);//---webix.ui结束----
// 组名搜索
$$("search1").attachEvent("onTimedKeyPress",function(){
        var value = this.getValue();
        $$("group").filter(function(obj){
          console.log(obj);
            return obj.gourp_name.indexOf(value)>=0;
        })
    });
// 操作员搜索
$$("search2").attachEvent("onTimedKeyPress",function(){
        var value = this.getValue(); ;
        $$("allOperate").filter(function(obj){
             return obj.machine_code.indexOf(value)>=0;
        })
 });
//把操作员入组信息传到后台
 var i=0;
  $$('group').attachEvent("onBeforeSelect", function(id, selection){
    var item = this.getItem(id).gourp_id;
      var sel = $$('group').getSelectedId();
      if(sel){
        var b=getSubmitOperatorData();
      }
      hidden1[i]=b;
	  console.log(b);
      submit(hidden2[i-1],hidden1[i]);
      i++;
   //获取当前点击的ID，和后台连接，得到对应组数据
    // webix.ajax().sync().post("getSuppliersCutterList", "gourp_name=" + item,function(text, xml, xhr) {
          // var json = $.parseJSON(text);
          // supplierData2 = json.data;
          //测试数据
      
      	var data1;
          webix.ajax().sync().post("getMachineByGourpId", "gourp_id="+item ,function(text, xml, xhr) {
      	    var postData = JSON.parse(text); 	
      	    data1=postData.data;
         });
          var data = new webix.DataCollection({data:data1});
          $$('operater').data.sync(data);
          $$("num").setValue($$("operater").count());
    // });
  });
// 页面初始化时访问后台，得到组列表和所有操作员列表
  // webix.ajax().sync().post("getSuppliersCutterList", "supplier_name=" + newv,function(text, xml, xhr) {
          // var json = $.parseJSON(text);
          // supplierData2 = json.data;
          //测试数据
          var group1;
           webix.ajax().sync().post("getGroupList", "",function(text, xml, xhr) {
        	    var postData = JSON.parse(text); 
        	    group1=postData.data;
           
           });
           //所有操作员数据
          var group2;
          webix.ajax().sync().post("getMachineList", "",function(text, xml, xhr) {
      	    var postData = JSON.parse(text); 
        	  group2=postData.data;
          });
          var data3 = new webix.DataCollection({data:group1});
          $$('group').data.sync(data3);
          var data2 = new webix.DataCollection({data:group2});
          $$('allOperate').data.sync(data2);
    // });
    $$("num").setValue($$("operater").count());
})

//添加一行
function addRow(){ 
  var dt=$$("group");
  var data=$$("add1").getValue();
  if(data==""){
    alert("请输入要添加的组号");
  }
  else{
    var id=dt.add({
      gourp_name:$$("add1").getValue()
    },0); 
     webix.ajax().sync().post("addGourp","gourp_name="+data,function(text, xml, xhr) {
   // 测试数据
    	 var retData = JSON.parse(text);
    	 if(retData.ret<0){
    		 alert(retData.msg);
    		 return;
    	 }
         webix.ajax().sync().post("getGroupList", "",function(text, xml, xhr) {
      	    var postData = JSON.parse(text); 
      	    group1=postData.data;
      	    var data = new webix.DataCollection({data:group1});
      	    $$('group').data.sync(data);
         });
 });
    var b=getSubmitOperatorData();
    submit(id,b);
  }
};
function clearAll(){
  $$('operater').clearAll();
  $$("num").setValue($$("operater").count());
}
//添加所有操作员
function addAll(){
var sel = $$('group').getSelectedId();
  if(sel){
    var item=Array();
    for(var i=0;i<$$("allOperate").count();i++){
      item[i]=$$('allOperate').getItem($$("allOperate").getIdByIndex(i)).machine_code+"/"+$$('allOperate').getItem($$("allOperate").getIdByIndex(i)).machine_id;
    }
    common(item);
  }
  else{
    alert("请先选择组");
  }
}
function common(item){
  var dt=$$("operater");
  var a=Array();
  var b=Array();
  var c = $$("operater").count();
  var flag2=Array();
  for(var j=0;j<item.length;j++){
      flag2[j]=true;
      for(var i=0;i<c;i++){
      a[i]=$$("operater").getIdByIndex(i);
      b[i]=$$('operater').getItem(a[i]).machine_code+"/"+$$('operater').getItem(a[i]).machine_id;
        if(b[i]==item[j]){
          flag2[j]=false;
          alert(b[i]+"已经在该组中");
        }
      }
    }
    for(var i=0;i<item.length;i++){ 
     if(flag2[i]){
	    	var arr = item[i].split("/");
        dt.add({
        	machine_code:arr[0],
        	machine_id:arr[1]
        });
      }
    }
  $$("num").setValue($$("operater").count());
} 
function addOne(){
	var sel = $$('group').getSelectedId();
	if(sel){  
	  var item=Array();
	  var dt=$$("operater");
	  var id = $$('allOperate').getSelectedId(); 
	  //目前操作员的数量
	  var c = $$("operater").count();
	  var a=Array();
	  var b=Array();
	  var flag1=true;
	  var flag2=Array();
	  if(typeof(id)=="string"){
	    var aaa=$$('allOperate').getItem(id).machine_code+"/"+$$('allOperate').getItem(id).machine_id;
	     for(var i=0;i<c;i++){
	      a[i]=$$("operater").getIdByIndex(i);
	      b[i]=$$('operater').getItem(a[i]).machine_code+"/"+$$('operater').getItem(a[i]).machine_id;
	      if(b[i]==aaa){
	        flag1=false;
	        alert(aaa+"已经在该组中");
	      }
	    }
	    if(flag1){
	    	var arr = aaa.split("/");
	      dt.add({
	    	  machine_code:arr[0],
	    	  machine_id:arr[1]
	      });
	    }
	  }
	  else{
	    for(var i=0;i<id.length;i++){
	      item[i] = $$('allOperate').getItem(id[i]).machine_code+"/"+$$('allOperate').getItem(id[i]).machine_id;
	    }
	    common(item);
	  }
	  $$("num").setValue($$("operater").count());
	  }
	  else{
	    alert("请先选择组");
	  }
	}
function clearOne(){
  var id = $$('operater').getSelectedId(); 
  $$('operater').remove(id);
  $$("num").setValue($$("operater").count());
}
function sure(){
  var id = $$('group').getSelectedId();
  //获取当时的组名 
  var item1 = $$('group').getItem(id).gourp_id;
  console.log(item1);
  // 获取对应的操作员
  var b=getSubmitOperatorData();
 if(b==""){
	 
	 alert($$('group').getItem(id).gourp_name+"未添加机台,请添加机台后再点击确认.");
	 return false;
	 
 }
  var params={};
  params.gourp_id=item1;
  params.machine_ids=b;
  //将数据传到后台
    webix.ajax().sync().post("editGourpMachineRef",params,function(text, xml, xhr) {
    		alert("执行成功");
    		location.reload();
   });
}
function submit(id,b){
  var sel = $$('group').getSelectedId(); 
  if(sel){
    if(id!=sel){
     //获取当时的组名 
      var item = $$('group').getItem(sel).gourp_id;
      var params={};
	  params.gourp_id=item;
	  params.machine_ids=b;
	   console.log(params);
  //将数据传到后台
    webix.ajax().post("editGourpMachineRef",params,function(text, xml, xhr) {
   });
    }
  }
}
function getSubmitOperatorData(){
	var c = $$("operater").count();
	  var str= "";
	  for(var i=0;i<c;i++){
			var operater = $$('operater').getItem($$("operater").getIdByIndex(i));
			var a = operater.machine_id;
			str+=a+",";
 	  }
	  return str;
	
}